<div align="left"><b><?=translate('title');?></b><br><br></div>
<?

#include('../rp2/global_functions.php');

include('wallet_db.html');

$error_1=translate('error_1');
$error_2=translate('error_2');
$error_3=translate('error_3');
$error_4=translate('error_4');
$settings_changed=translate('settings_changed');
$database_error=translate('database_error');
$availability=translate('availability');
$wallet_empty=translate('wallet_empty');
$no_settings=translate('no_settings');
$wallet_settings=translate('wallet_settings');
$email_not=translate('email_not');
$email_not_expl=translate('email_not_expl');
$auto_refill=translate('auto_refill');
$auto_refill_expl=translate('auto_refill_expl');
$trigger_amount=translate('trigger_amount');
$trigger_amount_expl=translate('trigger_amount_expl');
$charge_amount=translate('charge_amount');
$charge_amount_expl=translate('charge_amount_expl');
$submit=translate('submit');

if (isset($hid_var_sett) && $hid_var_sett==485) {
        #if (empty($_POST[mailalert_amount]) or !preg_match("/^\d*$/", $_POST[mailalert_amount])) {
        #        echo "<h4><font color=red>$error_1</font></h4>";
        #} elseif (isset($_POST[auto_refill]) && $_POST[auto_refill]=='y' && empty($_POST[auto_refill_min]) or !preg_match("/^\d*$/", $_POST[auto_refill_min])) {
        #        echo "<h4><font color=red>$error_2</font></h4>";
	#} elseif (isset($_POST[auto_refill]) && $_POST[auto_refill]=='y' && empty($_POST[auto_refill_amount]) or !preg_match("/^\d*$/", $_POST[auto_refill_amount])) {
        #        echo "<h4><font color=red>$error_3</font></h4>";
	#} elseif (isset($_POST[auto_refill]) && $_POST[auto_refill]=='y' && $auto_refill_amount < 200 or $auto_refill_amount > 3000) {
        #       	echo "<h4><font color=red>$error_4</font></h4>";
	if (!isset($_POST[mailalert_amount]) or !preg_match("/^\d*$/", $_POST[mailalert_amount])) {
                echo "<h4><font color=red>$error_1</font></h4>";
        } else {
                if (isset($_POST[auto_refill]) && $_POST[auto_refill]=='y') {
                        $auto_refill='t';
                        $auto_refill_min=$_POST[auto_refill_min];
                        $auto_refill_amount=$_POST[auto_refill_amount];
                } else {
                        $auto_refill='f';
                        $auto_refill_min='0';
                        $auto_refill_amount='0';
                }
		$sql="SELECT * FROM wallet_settings WHERE reseller_id='$id'";	
		$res=pg_query($dbh81,$sql);
		$num=pg_num_rows($res);
		unset($sql);
		if ($num==0) {
			$sql="INSERT INTO wallet_settings VALUES('$id','$_POST[mailalert_amount]','$_POST[mail_alert_currency]','$auto_refill','$_POST[auto_refill_currency]','$auto_refill_min','$auto_refill_amount','f')";
		} else {
                	$sql="UPDATE wallet_settings SET mailalert_amount='$_POST[mailalert_amount]', mail_alert_currency='$_POST[mail_alert_currency]', auto_refill='$auto_refill', auto_refill_currency='$_POST[auto_refill_currency]',auto_refill_min='$auto_refill_min',auto_refill_amount='$auto_refill_amount',mail_sent='f' WHERE reseller_id='$id'";
		}
                if ($result=exec_query($sql,$dbh81)) {
                        echo "<h4><font color=green>$settings_changed</font></h4>";
			$dbh_old=$dbh;
			$dbh=$dbh81;
			unset($sql);
			//------------------------------------------------------
			        $sql['type']='SELECT';
			        $sql['params']='first_name, last_name, email, name';
			        $sql['table']='resellers';
			        $sql['where']="reseller_id='$id'";
			        $sql['options']='';
			        $sql['return']='struct';
			//------------------------------------------------------
			if (!$result=sql_exec($sql,$dbh81)) {
			        exit("<h4><font color=red>$database_error</font></h4>");
			}
			$fullname=$result[0][first_name]." ".$result[0][last_name];
			$email[address]=$result[0][email];
			$nick_name=$result[0][name];
			$alert=sprintf("%01.2f",$_POST[mailalert_amount]);

			$SETTINGS["baseTMName"]="ResselersPanel Team";
	                $SETTINGS["baseSupportEmail"]="support@resellerspanel.com";
			$now_date=date("l, F j, Y \a\\t g:i A",strtotime("-2 hours")).", GMT";
	                $mail_array= getMail ("wallet_settings", Array(
	                                "fullname"=>$fullname,
	                                "date"=>$now_date,
	                                "username"=>$nick_name,
	                                "alert"=>$alert,
	                                "mainserver" => "ResellersPanel"
	                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

	                $email[from]= $mail_array[from];
	                $email[mime]= $mail_array[mime];
	                $email[subject]= $mail_array[subject];
	                $email[body]=$mail_array[body];
	                if (!mail($email[address],$email[subject],$email[body],
	                        "From: ".$email[from]."\r\n"
	                        ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
	                                $mail=serialize($email);
	                                mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
	                }
			$dbh=$dbh_old;
                } else {
                        exit("$database_error");
                }
        }
}
unset($sql);
//------------------------------------------------------
$sql['type']='SELECT';
$sql['params']='*';
$sql['table']='wallet';
$sql['where']="reseller_id='$id' and approved!='f'";
$sql['options']='ORDER BY currency ASC';
$sql['return']='struct';
//------------------------------------------------------
if ($result=sql_exec($sql,$dbh81,'assoc')) {
        echo "$availability";
        $available=array();
        foreach ($result as $value) {
                echo "<b>$value[amount] $value[currency]</b> ";
                $available[$value[currency]]=$value[amount];
        }
        $currencies_available=array_keys($available);
} else {
        echo "<h4>$wallet_empty</h4>";
}

unset($sql);
//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='curr';
        $sql['table']='currency';
        $sql['where']="";
        $sql['options']='';
        $sql['return']='struct';
//------------------------------------------------------
if ($result=sql_exec($sql,$dbh81,'assoc')){
        foreach($result as $value) {
                $currency[]=$value[curr];
        }
} else {
        exit("$database_error");
}


//------------------------------------------------------
$sql['type']='SELECT';
$sql['params']='*';
$sql['table']='wallet_settings';
$sql['where']="reseller_id='$id'";
$sql['options']='';
$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) echo "<p>$no_settings $username</p>";
        $value=$result[0];
        #exit(test_array($value));
        if ($value[auto_refill]=='t') {
                $chk='checked';
        } else {
                $chk='';
        }
	$params[sid] = $sid;
	$params[action] = "walletSettings";
	$url = "$baseURL/?".encode_params($params);
	echo "<script>
		function check_price (obj) {
			if (obj.mailalert_amount.value != \"\") return true;
			else {
				alert(\"Please fill in mailalert amount\");
				return false;
			}
		}
	</script>";
	$alert=sprintf("%01.2f",$value[mailalert_amount]);
        echo "<br><br><table class='bTable' cellspacing=0>
        <form action=\"$url\" method=\"POST\" onSubmit=\"return check_price(this)\">
        <tr>
                <th colspan=2>$wallet_settings</th>
        </tr>
        <tr>
                <td colspan=2>&nbsp;</td>
        </tr>
        <tr>
                <td align=right width=50%><b>$email_not</b><br>$email_not_expl</td>
                <td align=left width=50%>\$&nbsp;<input name=mailalert_amount type=text value=\"$alert\" size=4 maxlength=3>&nbsp;USD";
#        echo "   <select name=mail_alert_currency>";
#                foreach ($currency as $v) {
#                        if ($value[mail_alert_currency]==$v) {
#                                $ch='selected';
#                        } else {
#                                $ch='';
#                        }
#                        if (in_array($v,$currencies_available)) echo "<option value=$v $ch>$v</option>\n";
#                }
#                echo "</select></td>
	echo "<input type=hidden name=mail_alert_currency value='USD'>
        </tr>";
/*     echo "<tr>
                <td align=right width=50%><b>$auto_refill</b><br>$auto_refill_expl</td>
                <td align=left width=50%><input name=auto_refill type=checkbox value=\"y\" $chk></td>
        </tr>
        <tr>
                <td align=right width=50%><b>$trigger_amount</b><br>$trigger_amount_expl</td>
                <td align=left width=50%><input name=auto_refill_min type=text value=\"$value[auto_refill_min]\" size=4 maxlength=3>&nbsp;
                <select name=auto_refill_currency>";

                foreach ($currency as $v) {
                        if ($value[auto_refill_currency]==$v) {
                                $ch='selected';
                        } else {
                                $ch='';
                        }
                        if (in_array($v,$currencies_available)) echo "<option value=$v $ch>$v</option>\n";
                }
                echo "</select></td>
        </tr>
        <tr>
                <td align=right width=50%><b>$charge_amount</b><br>$charge_amount_expl</td>
                <td align=left width=50%><input name=auto_refill_amount type=text value=\"$value[auto_refill_amount]\" size=5 maxlength=4></td>
        </tr>";
*/
echo "<tr>
                <td colspan=2>&nbsp;</td>
        </tr>
        <tr>
                <td colspan=2 align=center><input type=submit name=submit value=\"$submit\"><input type=hidden name=hid_var_sett value=485></td>
        </tr>
        </form>
</table>";
?>
